package cn.springboot.erp.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.springboot.erp.commons.QueryParams;
import cn.springboot.erp.commons.ResponseResult;
import cn.springboot.erp.entity.Warehouses;
import cn.springboot.erp.service.WarehousesService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "仓库管理")
@RestController
@RequestMapping("warehouse")
public class WarehouseController {

    @Resource
    private WarehousesService warehouseService;

    @ApiOperation("仓库名称列表")
    @GetMapping("warehouseList")
    public ResponseResult warehouseList(){
        LambdaQueryWrapper<Warehouses> lambda = new QueryWrapper<Warehouses>().lambda();
        lambda.select(Warehouses::getId,Warehouses::getName);
        List<Warehouses> warehouseList = warehouseService.list(lambda);
        return ResponseResult.Success().data("warehouseList",warehouseList);
    }

    @ApiOperation("数据分页")
    @PostMapping("page")
    public ResponseResult listPage(@RequestBody QueryParams<Warehouses> queryParams){
        return warehouseService.listPage(queryParams);
    }

    @ApiOperation("根据id查询")
    @GetMapping("selectById/{id}")
    public ResponseResult selectById(@PathVariable("id") Integer id){
        Warehouses warehouse = warehouseService.getById(id);
        return ResponseResult.Success().data("warehouse",warehouse);
    }

    @ApiOperation("根据id删除")
    @GetMapping("deleteById/{id}")
    public ResponseResult deleteById(@PathVariable("id") Integer id){
        warehouseService.deleteById(id);
        return ResponseResult.Success().msg("删除成功");
    }

    @ApiOperation("添加或编辑")
    @PostMapping("insertOrUpdate")
    public ResponseResult insertOrUpdate(@RequestBody Warehouses warehouses){
        // 根据仓库名称查询
        String name = warehouses.getName();
        LambdaQueryWrapper<Warehouses> lambda = new QueryWrapper<Warehouses>().lambda();
        lambda.eq(Warehouses::getName,name);
        Warehouses one = warehouseService.getOne(lambda);

        if (ObjectUtils.isNotNull(one)){
            if (ObjectUtil.equal(one.getId(),warehouses.getId())){
                warehouseService.saveOrUpdate(warehouses);
                return ResponseResult.Success().msg("修改成功");
            }
            return ResponseResult.Fail().msg("仓库名已经存在");
        }
        warehouseService.saveOrUpdate(warehouses);
        return ResponseResult.Success().msg("添加成功");
    }

    @ApiOperation("修改状态")
    @GetMapping("state/{id}/{state}")
    public ResponseResult state(@PathVariable("id") Integer id,@PathVariable("state") Integer state){
        Warehouses warehouses = new Warehouses();
        warehouses.setId(id);
        warehouses.setState(state);
        // 修改状态
        warehouseService.updateById(warehouses);
        System.out.println("---   state = "+warehouses.getState());
        String str = null;
        if (warehouses.getState()==1){
            str="禁用";
        }else str="正常";
        return ResponseResult.Success().msg(str);
    }

}
